package com.lagou.k8s.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.lagou.k8s.pojo.Position;
import com.lagou.k8s.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 职位控制类
 * @author yzh
 */
@RestController
@RequestMapping("position")
public class PositionController {

    @Autowired
    private PositionService<Position> positionService;

    /**
     * 获取所有职位
     * @return 获取结果
     */
    @GetMapping("getAllPosition")
    public R getHotPosition() {
        try {
            List<Position> allPosition = positionService.list();
            return R.ok(allPosition);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("查询失败");
        }
    }

    /**
     * 根据ID获取职位
     * @param id 职位ID
     * @return 获取结果
     */
    @GetMapping("getPositionById/{id}")
    public R getPositionById(@PathVariable Integer id) {
        try {
            Position position = positionService.getById(id);
            return R.ok(position);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("查询失败");
        }
    }

    /**
     * 保存职位
     * @param position 职位
     * @return 保存结果
     */
    @PostMapping("savePosition")
    public R savePosition(@RequestBody Position position) {
        try {
            positionService.save(position);
            return R.ok("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("保存失败");
        }
    }

    /**
     * 更新职位
     * @param position 职位
     * @return 更新结果
     */
    @PostMapping("updatePosition")
    public R updatePosition(Position position) {
        try {
            positionService.updateById(position);
            return R.ok("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("更新失败");
        }
    }

    /**
     * 根据ID删除职位
     * @param id 职位ID
     * @return 删除结果
     */
    @GetMapping("deletePositionById/{id}")
    public R deletePositionById(@PathVariable Integer id) {
        try {
            positionService.removeById(id);
            return R.ok("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("删除失败");
        }
    }
}